how to map and flatten an array in javascript